class IndexController < ApplicationController
 before_filter :frist_time,:except=>['sinup','create']
 before_filter :nav
  #layout "index"
 layout :theme_layout_func
 
  def index
    
    @lastest_topic = Topic.find(:first,:order => "created_at DESC")
    @topics__pages=Topic.find(:all,:order => "istop DESC,created_at DESC") 
    @topic_pages, @topics = paginate_collection @topics__pages, {:page => @params[:page],:per_page =>getconfig("normal_shownum").value.to_i,:order => "created_at DESC" }
  end
  
  
  def frist_time
    if User.count == 0 
      redirect_to(:action => "sinup")
    end
  end
  
  def sinup
  #@user_groups = UserGroup.find(:all)
  @user = User.new
  end
  

  def create
    #@params['user']['content_type'] = @params['user']['tmp_file'].content_type 
    # This makes sure filenames are sane
    #@params['user']['data'] = @params['user']['tmp_file'].read
    #@params['user'].delete('tmp_file') 
    # let's remove the field from the hash, because there's no such field in the DB anyway.
    @user = User.new(@params['user'])
    # then the basic if @person.save ... like in <a href="http://wiki.rubyonrails.org/rails/pages/TutorialFramingOut" class="existingWikiWord">TutorialFramingOut</a>
    @user.user_group_id = 1 if User.count == 0 
    if @user.save
      flash[:notice] = 'User was successfully created.'
      redirect_to(:action=>"login")
    else
      flash[:notice] = 'User was not created,try angin..'
      render :action => 'sinup'
    end
  end
  
  def search
    @topics=Topic.find(:all,:conditions=>["title LIKE ? or body like ?","%" + params[:keys]+"%","%" + params[:keys]+"%"],
    :order => "created_at DESC",:limit=>20) 
    render :layout => false
  end
  
  
  def login
      if request.get?
        session[:user_id] = nil
        session[:user_group_id] = nil
        @user = User.new
      else
        @user=User.new(@params[:user])
        logged_in_user = @user.try_to_login
        if logged_in_user
          session[:user_id]=logged_in_user.id
          session[:user_group_id] = logged_in_user.user_group_id
          jumpto = session[:jumpto] || {:controller=>"admin/topic",:action=> "index"}
          session[:jumpto] = nil
          flash[:notic] = "登陆成功，欢迎您..."
          redirect_to(jumpto)
        else
          flash[:notice]="对不起,您输入的账号和密码不匹配..."
          redirect_to(:action => "login")
        end
      end
  end

  #�û��ǳ�
  def logout
    @user=User.find_by_id(session[:user_id])
    session[:user_id] = nil
    flash[:notice] ="欢迎您的光临,您已经成功登出,期待您的下次光临..."
    redirect_to(:controller=>"index",:action=>"index")
  end
  
  def theme
  
  end

end
